unit dic_oneforfour;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, dynlibs, IBConnection, sqldb, FileUtil, Forms, Controls, Graphics,
  Dialogs, DBGrids, Buttons,udata,LCLType, StdCtrls,db;

type

  { Tdic_oneforfive }

  Tdic_oneforfive = class(TForm)
    add: TBitBtn;
    bupdate: TBitBtn;
    find: TEdit;
    save: TBitBtn;
    cancel: TBitBtn;
    copypaste: TBitBtn;
    delete: TBitBtn;
    print: TBitBtn;
    help: TBitBtn;
    DBGrid1: TDBGrid;
    procedure addClick(Sender: TObject);
    procedure DBGrid1DblClick(Sender: TObject);
    procedure deleteClick(Sender: TObject);
    procedure findChange(Sender: TObject);
    procedure findKeyPress(Sender: TObject; var Key: char);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyPress(Sender: TObject; var Key: char);
    procedure printClick(Sender: TObject);
    procedure saveClick(Sender: TObject);
    procedure cancelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure CaseSelect();
  private
    { private declarations }
  public
    { public declarations }
  end;

 TDicPrintProc=procedure(textSQL:UTF8String);StdCall;

var
  dic_oneforfive: Tdic_oneforfive;
  insorupdate:Integer;
  lib:TLibHandle;
  DicPrint : TDicPrintProc;

implementation

uses umain, unova;
{$R *.lfm}

{ Tdic_oneforfive }


procedure Tdic_oneforfive.cancelClick(Sender: TObject);
begin
    DataModule1.DIC_SQLTransaction.Rollback;
    find.Visible:=false;
  Close;
end;

procedure Tdic_oneforfive.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  DataModule1.DIC_SQLTransaction.Rollback;
  udata.DataModule1.DIC_SQLQuery.Close;
  udata.DataModule1.DIC_SQLQuery.SQL.Clear;
end;

procedure Tdic_oneforfive.addClick(Sender: TObject);
begin
  insorupdate:=Integer((Sender as TBitBtn).Tag);
  nova.ShowModal;
end;

procedure Tdic_oneforfive.DBGrid1DblClick(Sender: TObject);
begin
  if find.Visible=false then begin
    find.Visible:=true;
    find.SetFocus;
  end
  else
  begin
    find.Visible:=false;
  end;
end;

procedure Tdic_oneforfive.deleteClick(Sender: TObject);
begin
if  MessageDlg('Попередження','Дійсно видалити цей запис?',mtConfirmation, mbYesNo,0)=mrYes then
begin
  with DataModule1.DIC_SQLQueryWRITE do begin
     case WhatIsForm of
        1:sql.Text:='delete from DIC_OZNAKI where id= '+
                      DataModule1.DIC_SQLQuery.FieldByName('ID').AsString;
        2:sql.Text:='delete from DIC_KATEGORIYA where id= '+
                      DataModule1.DIC_SQLQuery.FieldByName('ID').AsString;
        3:sql.Text:='delete from DIC_POSADI where id= '+
                      DataModule1.DIC_SQLQuery.FieldByName('ID').AsString;
        4:sql.Text:='delete from DIC_PIDROZDIL where id= '+
                      DataModule1.DIC_SQLQuery.FieldByName('ID').AsString;
        5:sql.Text:='delete from DIC_PLANRAH where id= '+
                      DataModule1.DIC_SQLQuery.FieldByName('ID').AsString;
     end
     end;
  try
     udata.DataModule1.DIC_SQLQueryWRITE.ExecSQL;
  except
    if MessageDlg('Помилка','Помилка видалення',mtError,[mbOK],0)= mrOK then;
  end;
     udata.DataModule1.DIC_SQLQuery.Close;
     udata.DataModule1.DIC_SQLQuery.SQL.Clear;
     dic_oneforfive.CaseSelect();
end;
end;

procedure Tdic_oneforfive.findChange(Sender: TObject);
begin
   udata.DataModule1.DIC_SQLQuery.Locate('NAZVA',find.Text,[loCaseInsensitive,loPartialKey]);
end;

procedure Tdic_oneforfive.findKeyPress(Sender: TObject; var Key: char);
begin
  if find.Visible=true then
     if Key=#27 then find.Visible:=false ;
end;

procedure Tdic_oneforfive.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (ssCtrl in Shift) and (Key=VK_RETURN) then save.Click;
  if (Key=VK_F1) then help.Click;
  if (ssCtrl in Shift) and (Key=VK_P) then print.Click;
  if (Key=VK_F8) then delete.Click;
  if (Key=VK_F5) then copypaste.Click;
  if (Key=VK_F2) then bupdate.Click;
  if (Key=VK_INSERT) then add.Click;
  if (Key=VK_F3) then DBGrid1DblClick(Sender);
end;

procedure Tdic_oneforfive.FormKeyPress(Sender: TObject; var Key: char);
begin
  if find.Visible=false then
     if Key=#27 then Close;
end;

procedure Tdic_oneforfive.printClick(Sender: TObject);
begin
   DataModule1.DIC_SQLTransaction.CommitRetaining;
 try
  lib:=LoadLibrary(INI.ReadString('MODULES','PRINTLIB',''));
  if lib=dynlibs.NilHandle then begin
    ShowMessage('Не вдається завантажити бібліотеку друку!'+#13+INI.ReadString('MODULES','PRINTLIB',''));
    Exit;
  end;
  DicPrint:=TDicPrintProc(GetProcAddress(lib,'DicPrint'));
  if DicPrint<>nil then DicPrint(DataModule1.DIC_SQLQuery.SQL.Text);
 finally
   if lib <>  DynLibs.NilHandle then if FreeLibrary(lib) then lib:= DynLibs.NilHandle;
  end;
end;

procedure Tdic_oneforfive.saveClick(Sender: TObject);
begin
    DataModule1.DIC_SQLTransaction.Commit;
    Close;
end;

procedure Tdic_oneforfive.FormShow(Sender: TObject);
begin
     udata.DataModule1.DIC_SQLQuery.Close;
     udata.DataModule1.DIC_SQLQuery.SQL.Clear;
     CaseSelect();
end;

procedure Tdic_oneforfive.CaseSelect;
begin
     case WhatIsForm of
   1:
     begin
        udata.DataModule1.DIC_SQLQuery.SQL.Text:='select ID as "Код", NAZVA as "Ознака" from dic_oznaki';
        udata.DataModule1.DIC_SQLQuery.Active:=true;
        DBGrid1.Columns[0].Visible:=false;
        dic_oneforfive.Caption:= DBGrid1.Columns[1].Title.Caption;
        DBGrid1.Columns[1].Width:=DBGrid1.Width-33;
     end;
   2:
     begin
        udata.DataModule1.DIC_SQLQuery.SQL.Text:='select ID as "Код", NAZVA as "Категорія" from dic_kategoriya';
        udata.DataModule1.DIC_SQLQuery.Active:=true;
        DBGrid1.Columns[0].Visible:=false;
        dic_oneforfive.Caption:= DBGrid1.Columns[1].Title.Caption;
        DBGrid1.Columns[1].Width:=DBGrid1.Width-33;
     end;
   3:
     begin
        udata.DataModule1.DIC_SQLQuery.SQL.Text:='select ID as "Код", NAZVA as "Посада" from dic_posadi';
        udata.DataModule1.DIC_SQLQuery.Active:=true;
        DBGrid1.Columns[0].Visible:=false;
        dic_oneforfive.Caption:= DBGrid1.Columns[1].Title.Caption;
        DBGrid1.Columns[1].Width:=DBGrid1.Width-33;
     end;
   4:
     begin
        udata.DataModule1.DIC_SQLQuery.SQL.Text:='select ID as "Код", NAZVA as "Підрозділ" from dic_pidrozdil';
        udata.DataModule1.DIC_SQLQuery.Active:=true;
        DBGrid1.Columns[0].Visible:=false;
        dic_oneforfive.Caption:= DBGrid1.Columns[1].Title.Caption;
        DBGrid1.Columns[1].Width:=DBGrid1.Width-33;
     end;
   5:
     begin
        udata.DataModule1.DIC_SQLQuery.SQL.Text:='select ID as "Код",RAHUNOK as "Рахунок", NAZVA as "Назва рахунку" from dic_planrah';
        udata.DataModule1.DIC_SQLQuery.Active:=true;
        DBGrid1.Columns[0].Visible:=false;
        dic_oneforfive.Caption:= DBGrid1.Columns[1].Title.Caption;
        DBGrid1.Columns[1].Width:=50;
        dic_oneforfive.Caption:= DBGrid1.Columns[2].Title.Caption;
        DBGrid1.Columns[2].Width:=DBGrid1.Width-DBGrid1.Columns[1].Width-33;
     end;
   else
     ShowMessage(IntToStr(WhatIsForm));
     end;
end;

end.

